package com.llkj.contractmanagement.utils;

import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static Calendar getCalendarByDate(Date date) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        return cl;
    }

    /**
     * 设置时间
     *
     * @param year
     * @param month
     * @param date
     * @return
     */
    public static Calendar setCalendar(int year, int month, int date) {
        Calendar cl = Calendar.getInstance();
        cl.set(year, month - 1, date);
        return cl;
    }

    /**
     * 获取当前时间的前一天时间
     *
     * @param cl
     * @return
     */
    public static Calendar getBeforeDay(Calendar cl) {
        // 使用roll方法进行向前回滚
        // cl.roll(Calendar.DATE, -1);
        // 使用set方法直接进行设置
        int day = cl.get(Calendar.DATE);
        cl.set(Calendar.DATE, day - 1);
        return cl;
    }

    /**
     * 获取当前时间的后一天时间
     *
     * @param cl
     * @return
     */
    public static Calendar getAfterDay(Calendar cl) {
        // 使用roll方法进行回滚到后一天的时间
        // cl.roll(Calendar.DATE, 1);
        // 使用set方法直接设置时间值
        int day = cl.get(Calendar.DATE);
        cl.set(Calendar.DATE, day + 1);
        return cl;
    }

    /**
     * 获取某个日期
     *
     * @return int[]
     * 返回int数组，年月日
     */
    public static int[] getDateArray(Calendar c) {
        int date[] = new int[3];
        date[0] = c.get(Calendar.YEAR);
        date[1] = c.get(Calendar.MONTH) + 1;
        date[2] = c.get(Calendar.DAY_OF_MONTH);
        return date;
    }

    /**
     * 获取当日的日期
     *
     * @return int[]
     * 返回int数组，年月日
     */
    public static int[] getTodayDateArray() {
        int date[] = new int[3];
        Calendar calendar = Calendar.getInstance();
        date[0] = calendar.get(Calendar.YEAR);
        date[1] = calendar.get(Calendar.MONTH) + 1;
        date[2] = calendar.get(Calendar.DAY_OF_MONTH);
        return date;
    }

    /**
     * 获取当日的日期
     *
     * @return Calendar
     */
    public static Calendar getTodayDate() {
        Calendar calendar = Calendar.getInstance();
        return calendar;
    }

    public static String getWeek(Calendar c) {
        String week = "星期";
        switch (c.get(Calendar.DAY_OF_WEEK)) {
            case 1:
                week += "天";
                break;
            case 2:
                week += "一";
                break;
            case 3:
                week += "二";
                break;
            case 4:
                week += "三";
                break;
            case 5:
                week += "四";
                break;
            case 6:
                week += "五";
                break;
            case 7:
                week += "六";
                break;
            default:
                break;
        }
        return week;

    }

    /**
     * 打印时间
     *
     * @param cl
     */
    public static void printCalendar(Calendar cl) {
        int year = cl.get(Calendar.YEAR);
        int month = cl.get(Calendar.MONTH) + 1;
        int day = cl.get(Calendar.DATE);
        System.out.println(year + "-" + month + "-" + day);
    }

}
